<?php
class ProgramsController extends AppController {

	var $name = 'Programs';
	var $helpers = array('Html', 'Form' );

	function index() {
		$this->Program->recursive = 0;
		$this->set('programs', $this->Program->findAll());
	}

	function view($id = null, $program = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Program.');
			$this->redirect('/programs/index');
			return;
		}
		if ($program == null) {
			$this->redirect('programs/view/'.$id.'/program.pdf');
			return;
		}
		$this->layout = 'pdf';
		$program = $this->Program->read(null, $id);
		$this->set('pdfData', $program['Program']['data']);
	}

	function admin_add() {
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			$tmpName = $this->params['form']['File']['tmp_name'];
			if (!is_uploaded_file($tmpName)) {
				$this->Session->setFlash("Error uploading file. Try again");
				$this->render();
				return;
			}

			$fileData = fread(fopen($tmpName, "r"), $this->params['form']['File']['size']);
			$this->data['Program']['data'] = $fileData;
			if ($this->Program->save($this->data)) {
				$this->Session->setFlash('The Program has been saved');
				$this->redirect('/admin/programs');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Program');
			$this->redirect('/admin/programs');
		}
		if ($this->Program->del($id)) {
			$this->Session->setFlash('The Program deleted: id '.$id.'');
			$this->redirect('/admin/programs');
		}
	}

	function admin_index() {
		$this->Program->recursive = 0;
		$this->set('programs', $this->Program->findAll());
	}
}
?>
